package com.machine.filling.function.set.orderList;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.machine.filling.R;
import com.machine.filling.db.modle.ExceptionOrderModle;
import com.machine.filling.db.modle.TempOrderModle;
import com.machine.filling.util.DateUtils;

import java.util.List;

/**
 * Created by Administrator on 2018/2/3 0003.
 */

public class OrderListAdapter extends RecyclerView.Adapter<OrderListAdapter.Holder> {
    private List<TempOrderModle> datas;
    protected Context mContext;
    protected IOnItemClickListener onItemClickListener;

    public OrderListAdapter(Context mContext, List<TempOrderModle> datas) {
        this.mContext = mContext;
        this.datas = datas;
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_order_list, parent, false);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(Holder holder, final int position) {
        if ("1".equals(datas.get(position).getOilCardType())) {//1用户的加油卡 2员工卡
            holder.tv_card_type.setText("用户卡");
        } else if ("2".equals(datas.get(position).getOilCardType())) {
            holder.tv_card_type.setText("员工卡");
        } else {
            holder.tv_card_type.setText("1".equals(datas.get(position).getOrderType()) ? "" : "未知卡");
        }

        holder.tv_user_name.setText(datas.get(position).getCustomName());
        holder.tv_fueled_money.setText(datas.get(position).getFuelMoney() == null ? "0" + "元" : datas.get(position).getFuelMoney() + "元");
        holder.tv_fueled_volume.setText(datas.get(position).getFuelVolume() == null ? "0" + "升" : datas.get(position).getFuelVolume() + "升");
        switch (datas.get(position).getPayMode()) {//支付方式  1支付宝 2微信 3现金 4油卡
            case "1":
                holder.tv_pay_type.setText("支付宝");
                break;
            case "2":
                holder.tv_pay_type.setText("微信");
                break;
            case "3":
                holder.tv_pay_type.setText("现金");
                break;
            case "4":
                holder.tv_pay_type.setText("油卡");
                break;
            default:
                holder.tv_pay_type.setText("");
                break;
        }
        // 1未支付  2已支付未写卡  3已支付
        switch (datas.get(position).getPayState()) {
            case "1":
                holder.tv_order_state.setText("未支付");
                holder.tv_card_type.setTextColor(mContext.getResources().getColor(R.color.content_red));
                holder.tv_user_name.setTextColor(mContext.getResources().getColor(R.color.content_red));
                holder.tv_fueled_money.setTextColor(mContext.getResources().getColor(R.color.content_red));
                holder.tv_fueled_volume.setTextColor(mContext.getResources().getColor(R.color.content_red));
                holder.tv_pay_type.setTextColor(mContext.getResources().getColor(R.color.content_red));
                holder.tv_order_state.setTextColor(mContext.getResources().getColor(R.color.content_red));
                holder.tv_fueled_time.setTextColor(mContext.getResources().getColor(R.color.content_red));
                break;
            case "2":
                holder.tv_order_state.setText("未写卡");
                holder.tv_card_type.setTextColor(mContext.getResources().getColor(R.color.content_red));
                holder.tv_user_name.setTextColor(mContext.getResources().getColor(R.color.content_red));
                holder.tv_fueled_money.setTextColor(mContext.getResources().getColor(R.color.content_red));
                holder.tv_fueled_volume.setTextColor(mContext.getResources().getColor(R.color.content_red));
                holder.tv_pay_type.setTextColor(mContext.getResources().getColor(R.color.content_red));
                holder.tv_order_state.setTextColor(mContext.getResources().getColor(R.color.content_red));
                holder.tv_fueled_time.setTextColor(mContext.getResources().getColor(R.color.content_red));
                break;
            case "3":
                holder.tv_order_state.setText("已支付");
                holder.tv_card_type.setTextColor(mContext.getResources().getColor(R.color.content_black));
                holder.tv_user_name.setTextColor(mContext.getResources().getColor(R.color.content_black));
                holder.tv_fueled_money.setTextColor(mContext.getResources().getColor(R.color.content_black));
                holder.tv_fueled_volume.setTextColor(mContext.getResources().getColor(R.color.content_black));
                holder.tv_pay_type.setTextColor(mContext.getResources().getColor(R.color.content_black));
                holder.tv_order_state.setTextColor(mContext.getResources().getColor(R.color.content_black));
                holder.tv_is_order.setTextColor(mContext.getResources().getColor(R.color.content_black));
                holder.tv_fueled_time.setTextColor(mContext.getResources().getColor(R.color.content_black));
                break;
            default:
                holder.tv_order_state.setText("");
                holder.tv_card_type.setTextColor(mContext.getResources().getColor(R.color.content_red));
                holder.tv_user_name.setTextColor(mContext.getResources().getColor(R.color.content_red));
                holder.tv_fueled_money.setTextColor(mContext.getResources().getColor(R.color.content_red));
                holder.tv_fueled_volume.setTextColor(mContext.getResources().getColor(R.color.content_red));
                holder.tv_pay_type.setTextColor(mContext.getResources().getColor(R.color.content_red));
                holder.tv_order_state.setTextColor(mContext.getResources().getColor(R.color.content_red));
                holder.tv_is_order.setTextColor(mContext.getResources().getColor(R.color.content_red));
                holder.tv_fueled_time.setTextColor(mContext.getResources().getColor(R.color.content_red));
                break;
        }
        //1预约 0未预约
        holder.tv_is_order.setText("1".equals(datas.get(position).getOrderType()) ? "预约" : "--");
        holder.tv_fueled_time.setText(DateUtils.formatDateTimeYMDHMS(datas.get(position).getFuelingTime()));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onClickItem(position, datas.get(position));
            }
        });
    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    public class Holder extends RecyclerView.ViewHolder {

        private final TextView tv_card_type;
        private final TextView tv_user_name;
        private final TextView tv_fueled_money;
        private final TextView tv_fueled_volume;
        private final TextView tv_pay_type;
        private final TextView tv_order_state;
        private final TextView tv_is_order;
        private final TextView tv_fueled_time;

        public Holder(View itemView) {
            super(itemView);
            tv_card_type = (TextView) itemView.findViewById(R.id.tv_card_type);
            tv_user_name = (TextView) itemView.findViewById(R.id.tv_user_name);
            tv_fueled_money = (TextView) itemView.findViewById(R.id.tv_fueled_money);
            tv_fueled_volume = (TextView) itemView.findViewById(R.id.tv_fueled_volume);
            tv_pay_type = (TextView) itemView.findViewById(R.id.tv_pay_type);
            tv_order_state = (TextView) itemView.findViewById(R.id.tv_order_state);
            tv_is_order = (TextView) itemView.findViewById(R.id.tv_is_order);
            tv_fueled_time = (TextView) itemView.findViewById(R.id.tv_fueled_time);
        }
    }

    public interface IOnItemClickListener {
        void onClickItem(int position, TempOrderModle bean);
    }

    public void setOnItemClickListener(IOnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
}
